package com.paas.form.visualdev.controller;


import com.alibaba.fastjson2.JSONObject;
import com.paas.common.model.RestResponse;
import com.paas.common.page.PageBean;
import com.paas.common.util.BeanUtil;
import com.paas.common.util.StringUtil;
import com.paas.common.web.controller.BaseController;
import com.paas.form.visualdev.service.VisualDevDataService;
import com.paas.form.visualdev.util.DataRequest;
import com.paas.form.visualdev.vo.ListDataQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@RestController
@RequestMapping(value = "/form/visualdevData")
@Api(description = "可视开发数据处理", tags = "")
public class VisualDevDataController extends BaseController {


    @Autowired
    public VisualDevDataService visualDevDataService;


    @RequestMapping(value = "/queryListData",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "列表数据", notes = "列表数据")
    public RestResponse queryListData(@RequestBody ListDataQuery listDataQuery) {
        RestResponse restResponse = RestResponse.error("获取列表出错");
        if(BeanUtil.isNotEmpty(listDataQuery) && StringUtil.isNotEmpty(listDataQuery.getFormId())){
            if (listDataQuery.getCurrentPage() == null) {
                listDataQuery.setCurrentPage(1);
            }
            if (listDataQuery.getPageSize() == null|| (listDataQuery.getPageSize() > 100) || (listDataQuery.getPageSize() < 0)) {
                listDataQuery.setPageSize(10);
            }

            PageBean<Map<String,Object>> pageBean = visualDevDataService.getPageList(listDataQuery);
            restResponse = RestResponse.success().put("pageBean",pageBean);


        }else {
            restResponse = RestResponse.error("表单id为空");
        }
        return restResponse;


    }



    @PostMapping(value = "addData")
    @ResponseBody
    @ApiOperation(value = "数据新增", notes = "数据新增")
    public RestResponse addData (String formId,@RequestBody JSONObject jsonObject){
        if(StringUtil.isEmpty(formId)){
            return RestResponse.error("formId为空");
        }
        if(BeanUtil.isEmpty(jsonObject)){
            return RestResponse.error("数据对象为空");
        }
        DataRequest dataRequest = new DataRequest();
        dataRequest.setFormId(formId);
        dataRequest.setJsonObject(jsonObject);
        dataRequest.setType("1");
        RestResponse restResponse = visualDevDataService.insertOrEditDataItem(dataRequest);
        return restResponse;
    }

    @PostMapping(value = "editData")
    @ResponseBody
    @ApiOperation(value = "数据编辑", notes = "数据编辑")
    public RestResponse editData (String formId,String dataId,@RequestBody JSONObject jsonObject){
        if(StringUtil.isEmpty(formId)){
            return RestResponse.error("formId为空");
        }
        if(StringUtil.isEmpty(dataId)){
            return RestResponse.error("dataId为空");
        }
        if(BeanUtil.isEmpty(jsonObject)){
            return RestResponse.error("数据对象为空");
        }
        DataRequest dataRequest = new DataRequest();
        dataRequest.setFormId(formId);
        dataRequest.setDataId(dataId);
        dataRequest.setJsonObject(jsonObject);
        dataRequest.setType("2");
        RestResponse restResponse = visualDevDataService.insertOrEditDataItem(dataRequest);
        return restResponse;
    }





    @RequestMapping(value = "queryOneData",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取一条数据", notes = "获取一条数据")
    public RestResponse queryOneData (String formId,String dataId){
        RestResponse restResponse = null;
        if(StringUtil.isEmpty(formId)){
            return RestResponse.error("formId为空");
        }
        if(StringUtil.isEmpty(dataId)){
            return RestResponse.error("dataId为空");
        }

        Map<String,Object> dataItem = visualDevDataService.getDataItem(formId,dataId);
        restResponse = RestResponse.success().put("data",dataItem);
        return restResponse;
    }



    @RequestMapping(value = "delData",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "数据删除", notes = "数据删除")
    public RestResponse delData (String formId,String dataId){
        RestResponse restResponse = null;
        if(StringUtil.isEmpty(formId)){
            return RestResponse.error("formId为空");
        }
        if(StringUtil.isEmpty(dataId)){
            return RestResponse.error("dataId为空");
        }
        restResponse = visualDevDataService.delData(formId,dataId);
        return restResponse;
    }








}
